{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Here, we try to convert TIF file into RGB and save them"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TIFF File --> RGB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from rasterio.plot import reshape_as_image\n",
    "import rasterio\n",
    "import numpy as np\n",
    "from skimage.io import imsave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\MUSTAFAAKTAS\\Anaconda3\\envs\\MaskRCNN\\lib\\site-packages\\skimage\\io\\_io.py:141: UserWarning: D:/DATASET/SpaceNet/Staj_Icin_Hazirlanan/test1/1056test.png is a low contrast image\n",
      "  warn('%s is a low contrast image' % fname)\n"
     ]
    }
   ],
   "source": [
    "PATH = 'TIF Files Path'\n",
    "ResimAdlari = os.listdir(PATH)\n",
    "\n",
    "for i in range(len(ResimAdlari)):\n",
    "    TIF_PATH = PATH+'/'+str(ResimAdlari[i]) #We get all the files one by one\n",
    "    \n",
    "    with rasterio.open(TIF_PATH, 'r') as ds:\n",
    "        arr = ds.read() # Read TIF file\n",
    "        image = reshape_as_image(arr) # TIF file's dimentions were (bands, rows, columns),\n",
    "                                      # convert them to (rows, columns, bands)\n",
    "        yeni = np.copy(image) # Convert to numpy array\n",
    "        ratio = np.amax(yeni) / 256 # The image still cannot be used, because its bit is different.\n",
    "        img8 = (yeni / ratio).astype('uint8') # Here, we convert it to 24 bit.\n",
    "\n",
    "        imsave('Save Path'+str(i)+'test.png',img8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
